﻿using System;
using System.Collections.Generic;
using System.Json;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DX_DailyUtil_1._0._1.ks.ctrl.share;
using DX_DailyUtil_1._0._1.ks.objt;
using DX_DailyUtil_1._0._1.ks.objt.hitch;
using DX_DailyUtil_1._0._1.ks.util;
using DX_DailyUtil_1._0._1.SerRef_WCF_Hitch;

namespace DX_DailyUtil_1._0._1.ks.ctrl.hitch
{
    public class Deal_HitchRecord : Deal_Grid<hitch_record>
    {
        private WCF_HitchClient mClient = new WCF_HitchClient();
        private MainPage mMainPage = null;
        private hitch_record mTempHitch = null;

        public Deal_HitchRecord(MainPage aMainPage, DataGrid aDataGrid)
            : base(aDataGrid)
        {
            this.mMainPage = aMainPage;
        }

        public void prepareData()
        {
            mClient.get_All_hitch_record_By_none_In_EntityCompleted += load_hitch_records;
            mClient.get_All_hitch_record_By_none_In_EntityAsync();
        }

        /// <summary>
        /// load完record后再load其他子项
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        private void load_hitch_records(object sender, get_All_hitch_record_By_none_In_EntityCompletedEventArgs e)
        {
            this.mData = e.Result;
            this.updateView();

        }

        public void initData()
        {
        }


        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////


        public void addData()
        {
            this.mTempHitch = new hitch_record();
            this.updateView();

            mClient.get_All_user_By_department_In_EntityCompleted += load_user;
            mClient.get_All_user_By_department_In_EntityAsync(1, this.mTempHitch);

            mClient.get_All_machine_By_department_In_EntityCompleted += load_machine;
            mClient.get_All_machine_By_department_In_EntityAsync(1, this.mTempHitch);

            mClient.get_All_client_By_department_In_EntityCompleted += load_client;
            mClient.get_All_client_By_department_In_EntityAsync(1, this.mTempHitch);

            mClient.get_All_hitch_cause_By_none_In_EntityCompleted += load_hitch_cause;
            mClient.get_All_hitch_cause_By_none_In_EntityAsync(this.mTempHitch);

            this.MData.Add(this.mTempHitch);
        }

        private void load_hitch_cause(object sender, get_All_hitch_cause_By_none_In_EntityCompletedEventArgs e)
        {
            List<hitch_cause> _result = e.Result;
            hitch_record _target = (hitch_record)e.UserState;
            _target.RL_hitchCause = new List<RL_hitchCause>(_result.Count);
            for (int i = 0; i < _result.Count; i++)
            {
                RL_hitchCause _temp = new RL_hitchCause();
                _temp.hitch_cause = _result[i];
                _target.RL_hitchCause.Add(_temp);
            }
            this.updateView();
        }

        private void load_client(object sender, get_All_client_By_department_In_EntityCompletedEventArgs e)
        {
            List<client> _result = e.Result;
            hitch_record _target = (hitch_record)e.UserState;
            _target.RL_clientContract = new List<RL_clientContract>(_result.Count);
            for (int i = 0; i < _result.Count; i++)
            {
                RL_clientContract _temp = new RL_clientContract();
                _temp.client = _result[i];
                _target.RL_clientContract.Add(_temp);
            }
            this.updateView();
        }

        private void load_machine(object sender, get_All_machine_By_department_In_EntityCompletedEventArgs e)
        {
            List<machine> _result = e.Result;
            hitch_record _target = (hitch_record)e.UserState;
            _target.RL_hitchLocate = new List<RL_hitchLocate>(_result.Count);
            for (int i = 0; i < _result.Count; i++)
            {
                RL_hitchLocate _temp = new RL_hitchLocate();
                _temp.machine = _result[i];
                _target.RL_hitchLocate.Add(_temp);
            }
            this.updateView();
        }

        private void load_user(object sender, get_All_user_By_department_In_EntityCompletedEventArgs e)
        {
            List<user> _result = e.Result;
            hitch_record _target = (hitch_record)e.UserState;
            _target.RL_labeler = new List<RL_labeler>(_result.Count);
            _target.RL_dealer = new List<RL_dealer>(_result.Count);
            for (int i = 0; i < _result.Count; i++)
            {
                RL_labeler _temp_0 = new RL_labeler();
                _temp_0.user = _result[i];
                _target.RL_labeler.Add(_temp_0);

                RL_dealer _temp_1 = new RL_dealer();
                _temp_1.user = _result[i];
                _temp_1.deal_date = DateTime.Now;
                _target.RL_dealer.Add(_temp_1);
            }
            this.updateView();
        }




        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////


        public void saveData(object aData)
        {
            hitch_record _data = (hitch_record)aData;
            // 有主键id表示不是新录入的记录,就update
            if (_data.id > 0)
            {
                mClient.upt_One_hitch_record_By_NoneCompleted += upt_hitch_record_finish;
                mClient.upt_One_hitch_record_By_NoneAsync(_data, _data);
            }
            // 其他没主键id的,按新录入处理,就save
            else
            {
                for (int i = 0; i < _data.RL_clientContract.Count; i++ )
                {
                    if(_data.RL_clientContract[i].FK_To_state_choose != 4)
                    {
                        _data.RL_clientContract.RemoveAt(i);
                    }
                }

                for (int i = 0; i < _data.RL_dealer.Count; i++)
                {
                    if (_data.RL_dealer[i].FK_To_state_choose != 4)
                    {
                        _data.RL_dealer.RemoveAt(i);
                    }
                }

                for (int i = 0; i < _data.RL_hitchCause.Count; i++)
                {
                    if (_data.RL_hitchCause[i].FK_To_state_choose != 4)
                    {
                        _data.RL_hitchCause.RemoveAt(i);
                    }
                }

                for (int i = 0; i < _data.RL_hitchLocate.Count; i++)
                {
                    if (_data.RL_hitchLocate[i].FK_To_state_choose != 4)
                    {
                        _data.RL_hitchLocate.RemoveAt(i);
                    }
                }

                for (int i = 0; i < _data.RL_labeler.Count; i++)
                {
                    if (_data.RL_labeler[i].FK_To_state_choose != 4)
                    {
                        _data.RL_labeler.RemoveAt(i);
                    }
                }

                mClient.ins_One_hitch_record_By_NoneCompleted += ins_hitch_record_finish;
                mClient.ins_One_hitch_record_By_NoneAsync(_data);
            }


        }

        private void ins_hitch_record_finish(object sender, ins_One_hitch_record_By_NoneCompletedEventArgs e)
        {
            MessageBox.Show("一条新记录保存:" + e.Result);
        }

        private void upt_hitch_record_finish(object sender, upt_One_hitch_record_By_NoneCompletedEventArgs e)
        {
            MessageBox.Show("一条记录更改成功!");

            hitch_record _data = (hitch_record)e.UserState;
            _data.time_check = e.Result;
        }

        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////

        public void updateData()
        {

        }


    }
}
